//
//  LYCommentListViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/13.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYCommentListViewController.h"
#import "LYCommentListModel.h"
#import "LYCommentCell.h"

@interface LYCommentListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBarHeight;

@property (nonatomic, strong) NSArray *models;
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, copy) NSString *to_user_id;

@end

@implementation LYCommentListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"评价";
    
    [self setUpTableView];
    [self getCommentList];
}

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    
    [self setUpBottomBar];
}

#pragma mark - 网络请求
/**
 *  评论数据
 */
- (void)getCommentList{

    [MBProgressHUD showMessage:nil];
    [LYRequestTool getCommentListWithToken:LYToken type:self.type data_id:self.data_id success:^(NSArray<LYCommentListModel *> *models) {
        [MBProgressHUD hideHUD];
        self.models = models;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD hideHUD];
        
    }];
}

/**
 *  发表评论
 */
- (void)sendComment{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool sendCommentWithToken:LYToken type:self.type to_user_id:self.to_user_id data_id:self.data_id content:self.inputTextField.text star:0 order_sn:nil success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"评论成功"];
        self.inputTextField.text = nil;
        [self.inputTextField resignFirstResponder];
        [self getCommentList];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];

}


#pragma mark - 初始化

- (void)setUpBottomBar{

    if (self.type!=CommentTypeClass) {
        self.bottomBarHeight.constant =  60;
    }else{
    
        self.bottomBarHeight.constant = 0;
    }

}

- (void)setUpTableView{
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
}

#pragma mark - tableviewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYCommentCell";
    
    LYCommentCell *cell = [LYCommentCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYCommentListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
}

#pragma mark - tableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == CommentTypeClass) {
        return;
    }
    
    LYCommentListModel *model = self.models[indexPath.row];
    
    self.to_user_id = model.from_user_id;
    self.inputTextField.placeholder = [NSString stringWithFormat:@"回复%@",model.from_nick_name];
    [self.inputTextField becomeFirstResponder];
    
}

#pragma mark - 按钮点击

- (IBAction)sendBtnOnclick:(id)sender {
    
    if (self.inputTextField.text.length == 0) {
        [MBProgressHUD showOnlyText:@"请输入评论内容"];
        return;
    }
    
    [self sendComment];
}


@end
